package org.an.proj.contacts.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import java.util.ArrayList;
import java.util.List;
import org.an.ku.util.SimpleTextWatcher;
import org.an.ku.view.dialog.chose.ChooseDialog;
import org.an.ku.view.dialog.chose.ChooseDialogInfo;
import org.an.ku.view.fulllist.FullListAdapter;
import org.an.proj.contacts.R;
import org.an.proj.contacts.model.PhoneInfo;
import org.an.proj.contacts.ui.contact.view.ContactItemBlockView;

/**
 * Created by an on 2016/10/22.<br/>
 * 联系方式adapter
 */
public class PhoneAdapter extends FullListAdapter<PhoneInfo>
  implements ChooseDialog.OnSingleItemClickListener {
  private ChooseDialog mChooseDialog;
  private final List<ChooseDialogInfo> mPhoneTypeInfos = new ArrayList<>();
  private String[] mPhoneTypes;

  public PhoneAdapter(Context context) {
    super(context);
    init();
  }

  private void init() {
    mPhoneTypes = mContext.getResources().getStringArray(R.array.phone_types);
    mChooseDialog = new ChooseDialog(mContext);
    mChooseDialog.setTitle("请选择电话类型");

    int[] phoneTypesParams = mContext.getResources().getIntArray(R.array.phone_type_params);
    for (int i = 0; i < mPhoneTypes.length; i++) {
      mPhoneTypeInfos.add(new ChooseDialogInfo(null, i == 0, mPhoneTypes[i], phoneTypesParams[i]));
    }
    mChooseDialog.setData(mPhoneTypeInfos);
    mChooseDialog.setOnSingleItemClickListener(this);
  }

  @Override public View getView(final ViewGroup parent, final int position, final PhoneInfo info) {
    final ContactItemBlockView blockView = new ContactItemBlockView(mContext);
    blockView.setInputType(EditorInfo.TYPE_CLASS_PHONE);
    blockView.mBtnDel.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        // 将数据和view的处理直接放到adapter中处理
        getData().remove(position);
        // removeView不会移除divide所以还是 刷新吧
        //parent.removeView(blockView);
        notifyDataSetChanged();
      }
    });
    blockView.mTvPicker.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        mChooseDialog.bindTextView(blockView.mTvPicker);
        // 由于这里有多个
        resetPhoneTypeSelect(info.getType());
        mChooseDialog.setTag(position).show();
      }
    });
    blockView.setData(info.getDesc(), "请输入电话");
    blockView.mEdtInput.setText(info.getNum());
    blockView.mEdtInput.addTextChangedListener(new SimpleTextWatcher() {
      @Override public void afterTextChanged(String s) {
        info.setNum(s);
      }
    });
    return blockView;
  }

  /**
   * @param value 123
   */
  private void resetPhoneTypeSelect(int value) {
    for (int i = 0; i < mPhoneTypeInfos.size(); i++) {
      ChooseDialogInfo info = mPhoneTypeInfos.get(i);
      info.setSelect(value == (int) info.value);
    }
  }

  @Override public void onSingleItemClick(ChooseDialog dialog, int index, ChooseDialogInfo info) {
    int position = (int) mChooseDialog.getTag();
    PhoneInfo item = getItem(position);
    item.setType((int) info.value);
    item.setDesc(info.text);
  }
}
